package com.qf.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.PermissionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {
    @Autowired
    DtsRoleService roleService;
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    DtsPermissionService permissionService;

    @PostMapping("/permissions")
    public Object permissions(@RequestBody Map<String, Object> map) {

        Integer roleId = (Integer) map.get("roleId");
        ArrayList<String> permissions = (ArrayList<String>) map.get("permissions");
        if (roleService.isSuperAdmin(roleId)) {
            return ResponseUtil.ok();
        }
        permissionService.updatePermissions(roleId, permissions);
        return ResponseUtil.ok();
    }

    @GetMapping("/permissions")
    public Object permissions(Integer roleId) {
        //获取系统的权限
        List<Permission> permissions = PermissionUtil.listPermission(applicationContext, "com.qf.controller");
        //获取系统的权限列表
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissions);
        //获取当前角色的所有权限
        Set<String> assignedPermissions;
        if (roleService.isSuperAdmin(roleId)) {
            assignedPermissions = PermissionUtil.listPermissionString(permissions);
        } else {
            assignedPermissions = roleService.queryStringPermissions(roleId);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("systemPermissions", systemPermissions);
        data.put("assignedPermissions", assignedPermissions);
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "角色删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole role) {
        try {
            roleService.delete(role);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-2, e.getMessage());
        }

        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "角色修改")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole role) {
        role = roleService.updateRole(role);
        return ResponseUtil.ok(role);
    }

    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "角色添加")
    @PostMapping("create")
    public Object create(@RequestBody DtsRole role) {
        roleService.create(role);

        return ResponseUtil.ok(role);
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"}, button = "角色查询")
    @GetMapping("list")
    public Object list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                       String sort, String order, String rolename) {
        Map<String, Object> data = roleService.queryPage(page, limit, sort, order, rolename);

        return ResponseUtil.ok(data);
    }

    @GetMapping("/options")
    public Object role() {
        List<DtsRole> list = roleService.findAll();
        List<Map<String, Object>> data = list.stream().map(role -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", role.getId());
            map.put("label", role.getName());
            return map;
        }).collect(Collectors.toList());
        return ResponseUtil.ok(data);
    }
}
